package Comp355Final.main;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

public class TCPGatherChapterSelection {
	private String bookname = new String();
	private ArrayList<String> bookchapter_name = new ArrayList<String>();
	private ArrayList<String> bookchapter_url = new ArrayList<String>();
	private ArrayList<String> bookchapter_provider = new ArrayList<String>();
	private ArrayList<String> bookchapter_site = new ArrayList<String>();
	private String TCP_dest;
	private GUIInternetChapterSelection parent;
	public TCPGatherChapterSelection(String TCP_dest, GUIInternetChapterSelection parent)
	{
		this.TCP_dest=TCP_dest;
		this.parent=parent;
	}
	public ArrayList<String> get_bookchapter_site()
	{
		return bookchapter_site;
	}
	public ArrayList<String> get_bookchapter_provider()
	{
		return bookchapter_provider;
	}
	public ArrayList<String> get_bookchapter_url()
	{
		return bookchapter_url;
	}
	public ArrayList<String> get_bookchapter_name()
	{
		return bookchapter_name;
	}
	public boolean connect_toTarget() {
		HttpURLConnection conn = null;
	    OutputStreamWriter wr;
	    BufferedReader rd = null;
	    URL url;
	    try {
			url = new URL(TCP_dest);
			conn = (HttpURLConnection)url.openConnection();
			conn.setDoOutput(true);
			conn.setDoInput(true);
			conn.setRequestMethod("GET");
			//wr = new OutputStreamWriter(conn.getOutputStream());
		    //wr.write(data);
		    //wr.flush();
			rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
		} catch (Exception e) {
			return false;
		}
		String line;
		boolean success_connected = false;
		int respond_code;
		try {
			respond_code = conn.getResponseCode();
			//parent.settxtview("OK");
			if(respond_code!=HttpURLConnection.HTTP_OK&&respond_code!=HttpURLConnection.HTTP_ACCEPTED)
			{
				success_connected=true;
				//parent.settxtview("OK");
			}
		    while ((line = rd.readLine()) != null) {
		    	//System.out.println(line);
		    	
		    	if(line.contains("list(")&& !line.contains("chapterid"))
		    	{
		    		//System.out.println(line);
		    		String[] all_chs = line.split("list\\(");
		    		
		    		for(int z=0;z<all_chs.length;z++)
		    		{
		    			if(!all_chs[z].equals(""))
		    			{
			    			//System.out.println("line: "+z+" "+all_chs[z]);
			    			all_chs[z].replaceFirst("\\);", "");
			    			String[] tmp_result = all_chs[z].split(",");
			    			bookchapter_url.add(tmp_result[0]);
			    			this.bookchapter_provider.add(tmp_result[3].replace("\'", ""));
			    			this.bookchapter_site.add(tmp_result[2]);
			    			this.bookchapter_name.add(tmp_result[1].replace("\'", ""));
			    			//System.out.println(tmp_result[0]+" "+tmp_result[1]+" "+tmp_result[2]+" "+tmp_result[3]);
		    			}
		    			
		    		}
		    		//Utility.quick_dialog(bookchapter_url.size()+" "+bookchapter_url.get(0), parent);
		    	}
		    }
		} catch (Exception e) {

		}

		return true;
	}
}
